package chaoyue.study.io.stream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;

/**
 * 缓冲流
 * 如果构造方法中需要的参数是一个流的话，构造方法里面的这个流被称为节点流
 * 也就是被装饰的对象，外面负责包装的这个流，叫包装流
 * 关闭流只需要关闭最外层的就可以，里面的流会逐层关闭
 */
public class BufferedStream {
    public static void main(String[] args) throws Exception {
        // 写入bufferedStream
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("/Users/chaoyue/Downloads/out.txt"));
        bufferedOutputStream.write("chaoyue hello ".getBytes(StandardCharsets.UTF_8));
        bufferedOutputStream.flush();
        bufferedOutputStream.close();
        // 读取bufferedStream
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("/Users/chaoyue/Downloads/out.txt"));
        System.out.println(new String(bufferedInputStream.readAllBytes()));
        bufferedInputStream.close();
    }
}
